package com.europe.business.europebusiness.ui.adapter;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.bean.CompanyBean;

import java.util.List;

public class CompanyAdapter extends RecyclerView.Adapter {

    private static final int VIWE_TYPE_FOOT = 1;
    private static final int VIWE_TYPE_DATA = 0;
    private Context mContext;
    private List<CompanyBean> mList;
    private MORE_STATUS status;

    public CompanyAdapter(Context context, List<CompanyBean> list){
        mContext = context;
        mList = list;
    }

    public void setStatus(MORE_STATUS status){
        this.status = status;
        notifyDataSetChanged();
    }

    public void setData(List<CompanyBean> list){
        mList = list;
    }


    @NonNull
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        if(i == VIWE_TYPE_FOOT){
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_foot_company, viewGroup, false);
            FootViewHolder viewHolder = new FootViewHolder(view);
            return viewHolder;
        }else{
            View view = LayoutInflater.from(mContext).inflate(R.layout.item_company, viewGroup, false);
            CompanyViewHolder viewHolder = new CompanyViewHolder(view);
            return viewHolder;
        }

    }

    @Override
    public void onBindViewHolder(@NonNull RecyclerView.ViewHolder viewHolder, int i) {

        if( i != mList.size()){
            String name = mList.get(i).getName();
            String business = mList.get(i).getMainBusiness();
            String country = mList.get(i).getCountry();
            String city = mList.get(i).getCity();
            ((CompanyViewHolder)viewHolder).companyName.setText(name);
            ((CompanyViewHolder)viewHolder).mainBussiness.setText(business);
            ((CompanyViewHolder)viewHolder).area.setText(country + "-" + city);

//            ((CompanyViewHolder)viewHolder).item.setOnClickListener(new View.OnClickListener() {
//                @Override
//                public void onClick(View v) {
//                    itemClikLIstener.onCl();
//                }
//            });
        }else{
            ((FootViewHolder)viewHolder).setData(status);
        }

    }

    @Override
    public int getItemCount() {

        return mList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if(position == mList.size()){
            return VIWE_TYPE_FOOT;
        }else{
            return VIWE_TYPE_DATA;
        }

    }

    class CompanyViewHolder extends RecyclerView.ViewHolder{
        public TextView companyName;
        public TextView mainBussiness;
        public ImageView companyLogo;
        public TextView area;

        public CompanyViewHolder(@NonNull View itemView) {
            super(itemView);
            companyName = itemView.findViewById(R.id.company_name);
            mainBussiness = itemView.findViewById(R.id.company_main_business);
            companyLogo = itemView.findViewById(R.id.company_logo);
            area = itemView.findViewById(R.id.company_country_city);
        }
    }

    class FootViewHolder extends RecyclerView.ViewHolder{
        public TextView loading;
        public ProgressBar progressBar;
        public FootViewHolder(@NonNull View itemView) {
            super(itemView);
            loading = itemView.findViewById(R.id.loading);
            progressBar = itemView.findViewById(R.id.progress);
        }

        public void setData(MORE_STATUS status){
            switch (status){
                case LOADING:
                    progressBar.setVisibility(View.VISIBLE);
                    loading.setVisibility(View.VISIBLE);
                    loading.setText("正在加载");
                    break;
                case LOADED:
                    progressBar.setVisibility(View.GONE);
                    loading.setVisibility(View.GONE);
                    //moreTv.setText("正在加载");
                    break;
                case LOAD_COMPLETED:
                    progressBar.setVisibility(View.GONE);
                    loading.setVisibility(View.VISIBLE);
                    loading.setText("已经到底");
                    break;


            }

        }


    }

    public enum MORE_STATUS{
        LOADING,
        LOADED,
        LOAD_COMPLETED;


    }
}
